﻿<%@ WebHandler Language="C#" Class="indexchart" %>

using System;
using System.Web;
using KeaoBase;
using KeaoDataBase;
using System.Configuration;
using System.Data;
using System.Text;

public class indexchart : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string zhanDian = context.Request.QueryString["zhandian"];
        if (string.IsNullOrEmpty(zhanDian))
        {
            context.Response.Write("{}");
            return; 
        }

        BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}",ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
        if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
        {
            context.Response.Write("{}");
            return;
        }
        else
        {
            try
            {
                string createCondition = m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "入库物料", "", SearchConditionType.等于);
                DateTime kaiShi = DateTime.Today;
                DateTime jieShu = kaiShi.AddHours(23).AddMinutes(59).AddSeconds(59);
                createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                DataTable ruKuDt = m_DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,sum(jingzhong) as jingzhong", null, createCondition, "wuliaomingcheng,xinghao", null);
                createCondition = m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, "出库物料", "", SearchConditionType.等于);
                createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                DataTable chuKuDt = m_DB.GetDataTableByCondition("tbl_chengzhong", "wuliaomingcheng,xinghao,sum(jingzhong) as jingzhong", null, createCondition, "wuliaomingcheng,xinghao", null);
                createCondition = m_DB.CreateAndCondition("shijian", DataValueCommonType.日期时间, kaiShi.ToString("yyyy-MM-dd HH:mm:ss"), jieShu.ToString("yyyy-MM-dd HH:mm:ss"), SearchConditionType.between);
                string colS = string.Format("sum(gl1) as [0-5],sum(gl2) as [5-10],sum(gl3) as [10-20],sum(gl4) as [20-30],sum(fl) as 粉料,sum(shui) as 水");
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                DataTable banHeDt = m_DB.GetDataTableByCondition("tbl_banhezhan", colS, null, createCondition, null, null);
                StringBuilder returnStr1 = new StringBuilder();
                StringBuilder returnStr2 = new StringBuilder();
                returnStr1.Append("{");
                returnStr1.Append("\"categories_ruku\":[");
                returnStr2.Append("\"data_ruku\":[");
                if (ruKuDt.Rows.Count > 0)
                {
                    foreach (DataRow row in ruKuDt.Rows)
                    {
                        returnStr1.Append(string.Format("\"{0}{1}\",", row["wuliaomingcheng"], row["xinghao"]));
                        returnStr2.Append(string.Format("\"{0}\",", row["jingzhong"]));
                    }
                    returnStr1.Remove(returnStr1.Length - 1, 1);
                    returnStr1.Append("],");
                    returnStr2.Remove(returnStr2.Length - 1, 1);
                    returnStr2.Append("],");
                    returnStr1.Append(returnStr2);
                }
                else
                {
                    returnStr1.Append("],");
                    returnStr2.Append("],");
                    returnStr1.Append(returnStr2);
                }
                returnStr2.Remove(0, returnStr2.Length);
                returnStr1.Append("\"categories_chuku\":[");
                returnStr2.Append("\"data_chuku\":[");
                if (chuKuDt.Rows.Count > 0)
                {
                    foreach (DataRow row in chuKuDt.Rows)
                    {
                        returnStr1.Append(string.Format("\"{0}{1}\",", row["wuliaomingcheng"], row["xinghao"]));
                        returnStr2.Append(string.Format("\"{0}\",", row["jingzhong"]));
                    }
                    returnStr1.Remove(returnStr1.Length - 1, 1);
                    returnStr1.Append("],");
                    returnStr2.Remove(returnStr2.Length - 1, 1);
                    returnStr2.Append("],");
                    returnStr1.Append(returnStr2);
                }
                else
                {
                    returnStr1.Append("],");
                    returnStr2.Append("],");
                    returnStr1.Append(returnStr2);
                }

                returnStr2.Remove(0, returnStr2.Length);
                returnStr1.Append("\"categories_banhezhan\":[");
                returnStr2.Append("\"data_banhezhan\":[");
                if (banHeDt.Rows[0][0] != DBNull.Value)
                {
                    foreach (DataColumn col in banHeDt.Columns)
                    {
                        returnStr1.Append(string.Format("\"{0}\",", col.ColumnName));
                        returnStr2.Append(string.Format("{0}\"name\":\"{1}\",\"value\":\"{2}\"{3},", "{", col.ColumnName, banHeDt.Rows[0][col], "}"));
                    }
                    returnStr1.Remove(returnStr1.Length - 1, 1);
                    returnStr1.Append("],");
                    returnStr2.Remove(returnStr2.Length - 1, 1);
                    returnStr2.Append("]");
                    returnStr1.Append(returnStr2);

                }
                else
                {
                    returnStr1.Append("],");
                    returnStr2.Append("]");
                    returnStr1.Append(returnStr2);
                }
                
                returnStr1.Append("}");
                context.Response.Write(returnStr1);
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("解析首页图表数据异常。{0}", err.Message));
                context.Response.Write("{}");
            }
        }
       
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}